package com.fymxy.question_bank_management.controller;

import com.fymxy.question_bank_management.pojo.ApiResult;
import com.fymxy.question_bank_management.pojo.Request2teacher;
import com.fymxy.question_bank_management.service.Request2teacherService;
import com.fymxy.question_bank_management.service.TeacherService;
import com.fymxy.question_bank_management.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;

@RestController
public class TeacherRegisterController {

    @Autowired
    private Request2teacherService request2teacherService;

    @Autowired
    private TeacherService teacherService;

    @PostMapping("/teacher/register")
    public ApiResult addTeacherRequest(@RequestBody Request2teacher request2teacher){
        request2teacher.setRequestDate(new Date());
        for(String str : teacherService.getTeacherNicknames())
            if(str.equals(request2teacher.getTeacherNickname()))
                return ApiResultHandler.buildApiResult(400, "用户名已被占用", request2teacher.getTeacherNickname());
        int addTeacherRequestRes = request2teacherService.insert(request2teacher);//1成功，0失败
        if(addTeacherRequestRes == 1)
            return ApiResultHandler.buildApiResult(200, "提交成功，请等待审核", addTeacherRequestRes);
        else
            return ApiResultHandler.buildApiResult(300, "提交失败，请稍后再试", addTeacherRequestRes);
    }
}
